package br.edu.unilasalle.simon_api.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import br.edu.unilasalle.simon_api.dao.IClienteDao;
import br.edu.unilasalle.simon_api.domain.Cliente;

@Repository
public class ClienteDao extends GenericDao<Cliente, Long> implements IClienteDao {
	
	public List<Cliente> findAllCliente() {
        StringBuilder str = new StringBuilder();
        str.append("select new Cliente(o.id, o.nome, o.sobrenome, o.celular, o.ativo) from Cliente o ");
        str.append("order by o.nome, o.sobrenome");
        TypedQuery<Cliente> q = entityManager.createQuery(str.toString(), Cliente.class);
		return q.getResultList();
    }
	
	public List<Cliente> findAllByNomeOrSobrenome(String param) {
        StringBuilder str = new StringBuilder();
        str.append("select new Cliente(o.id, o.nome, o.sobrenome, o.celular, o.ativo) from Cliente o ");
        str.append("where (upper(o.nome) like :param or upper(o.sobrenome) like :param) ");
        str.append("order by o.nome, o.sobrenome");
        TypedQuery<Cliente> q = entityManager.createQuery(str.toString(), Cliente.class);
        q.setParameter("param", param.toUpperCase()+"%");
		return q.getResultList();
    }
	
}
